package utils;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.Map;

import zhang.automobileservice.R;


/**
 * Volley请求网络封装类
 * Created by Administrator on 2016/6/13.
 */
public class RequestQueueUtils {

    private static RequestQueue requestQueue;
    private static ImageLoader imageLoader;

    /**
     * 单利返回一个RequestQueue对象
     * @param context
     * @return
     */
    public static RequestQueue getRequestQueue(Context context) {
        if (requestQueue == null) {
            requestQueue = Volley.newRequestQueue(context);
        }
        return requestQueue;
    }

    /**
     * JesonRequest的get请求封装了OnErrorReponse对象
     * @param context
     * @param url
     * @param listener
     */
    public static void exectueJsonRequest(final Context context, String url, Response.Listener listener){
        getRequestQueue(context);
        JsonObjectRequest request = new JsonObjectRequest( url,null, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(context,"数据请求失败",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(request);
    }

    /**
     * JesonRequest的map请求
     * @param context
     * @param url
     * @param listener
     * @param errorlistener
     */
    public static void exectueJsonRequest(Context context, String url, Response.Listener listener, Response.ErrorListener errorlistener){
        getRequestQueue(context);
        JsonObjectRequest request = new JsonObjectRequest( url,null, listener, errorlistener);
        requestQueue.add(request);
    }
    public static void exectueStringRequest_post( final Context context, String url, Response.Listener <String>listener, final Map<String, String> map){
        getRequestQueue(context);
        StringRequest request= new StringRequest(Request.Method.POST,url,listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(context,"数据请求失败",Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //Toast.makeText(context,"数据请求失败",Toast.LENGTH_SHORT).show();
                return map;
            }
        };
        requestQueue.add(request);
    }

    /**
     * StringRequest的map请求
     * @param context
     * @param url
     * @param listener
     */
    public static void exectueStringRequest(final Context context, String url, Response.Listener <String>listener){
        getRequestQueue(context);
        StringRequest request= new StringRequest(url,listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(context,"数据请求失败",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(request);
    }

    /**
     * ImageLoader请求
     * @param url
     * @param listener
     *
            ImageListener listener = ImageLoader.getImageListener(imageView,
            R.drawable.default_image, R.drawable.failed_image);

     */
    public static void exectueImageLoader(Context context,String url,ImageLoader.ImageListener listener){
        getRequestQueue(context);
        if(imageLoader!=null){
            imageLoader = new ImageLoader(requestQueue, new MyImageCache() );
        }
        imageLoader.get(url,listener);
        //或者     imageLoader.get(url,listener，200，200);
    }
    /**
     * NetWorkImageView请求
     * @param context
     * @param url
     * @param networkImageView
     */
    public static void exectueNetWorkImageView(Context context,String url, NetworkImageView networkImageView){
        getRequestQueue(context);
        if(imageLoader==null){
            imageLoader = new ImageLoader(requestQueue, new MyImageCache() );
        }

            //networkImageView.setDefaultImageResId(R.drawable.default_ptr_rotate);
            //networkImageView.setErrorImageResId(R.drawable.downarrow_gray);
            networkImageView.setImageUrl(url, imageLoader);
        }


    public static void exectueCircleNetWorkImageView(Context context,String url, CircleNetworkImage networkImageView){
        getRequestQueue(context);
        if(imageLoader==null){
            imageLoader = new ImageLoader(requestQueue, new MyImageCache());}
        Log.e("传过来的头像地址","url");
        networkImageView.setDefaultImageResId(R.mipmap.picture);
        networkImageView.setErrorImageResId(R.mipmap.picture);
        networkImageView.setImageUrl(url, imageLoader);
    }
    public static void exectueJsonRequest_post(final Context context, String url, Response.Listener<JSONObject> listener, final Map<String,String> map){
        getRequestQueue(context);
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url,null, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("服务器连接失败","服务器连接失败");
                Toast.makeText(context,"数据请求失败",Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        requestQueue.add(request);
    }


}
